<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Category extends Backend_Controller {

	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
		// 加载category模型
		$this->load->model('backend/Category_model', 'Category');
		// 加载Album模型
		$this->load->model('backend/Album_model', 'Album');
		// 加载验证类
		$this->load->library('form_validation');
	}

	/**
	 * 显示后台个人中心下的查看分类
	 */
	public function index() {
		$this->isValidRequest();
		$result = $this->Category->getCategoryAll($this->page, $this->input->post());
		$this->load->view('backend/category/index', $result);
	}

	/**
	 * 添加分类
	 */
	public function add() {
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'name',
				'link_id',
				'active_id',
				'banner_id',
				'rank',
				'parent',
				'type'
					), array(
				array(
					'name',
					'required'
				),
				array(
					'link_id',
					'required|integer'
				),
				array(
					'active_id',
					'required|integer'
				),
				array(
					'banner_id',
					'required|integer'
				),
				array(
					'rank',
					'required|integer'
				),
				array(
					'parent',
					'required|integer'
				),
				array(
					'type',
					'required|in_list[0,1]'
				)
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'name' => $this->form_validation->name,
				'rank' => $this->form_validation->rank,
				'parent' => $this->form_validation->parent,
				'type' => $this->form_validation->type,
				'link_id' => $this->form_validation->link_id,
				'active_id' => $this->form_validation->active_id,
				'banner_id' => $this->form_validation->banner_id,
				'created_at' => time()
			);
			$insertId = $this->Category->addCategory($data);
			if ($insertId) {
				$ids = array (
					$data['link_id'],
					$data['active_id'],
					$data['banner_id'],
				);
				$result = $this->Album->update(array('source_id'=>$insertId), array('id'=>$ids));
				if ($result) {
					$this->renderJson(200, '', array(
						'navTabId' => 'backendcategoryindex',
						'callbackType' => 'closeCurrent'
					));
				}
			}
			$this->renderJson(300);
		}
		$this->isValidRequest();
		$this->load->view('backend/category/add');
	}

	/**
	 * 删除分类
	 */
	public function delete() {
		$ids = $this->input->post_get('ids');
		if ($this->Category->deleteCategory($ids)) {
			$this->renderJson(200);
		}

		$this->renderJson(300);
	}

	/**
	 * 修改分类信息
	 */
	public function edit() {
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'id',
				'link_id',
				'active_id',
				'banner_id',
				'name',
				'rank',
				'parent',
				'type'
					), array(
				array(
					'id',
					'integer|greater_than[1]'
				),
				array(
					'name',
					'required'
				),
				array(
					'link_id',
					'required|integer'
				),
				array(
					'active_id',
					'required|integer'
				),
				array(
					'banner_id',
					'required|integer'
				),
				array(
					'rank',
					'required|integer'
				),
				array(
					'parent',
					'required|integer'
				),
				array(
					'type',
					'required|in_list[0,1]'
				)
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'name' => $this->form_validation->name,
				'rank' => $this->form_validation->rank,
				'parent' => $this->form_validation->parent,
				'type' => $this->form_validation->type,
				'link_id' => $this->form_validation->link_id,
				'active_id' => $this->form_validation->active_id,
				'banner_id' => $this->form_validation->banner_id,
			);

			if ($this->Category->updateCategory($data, $this->form_validation->id)) {
				$this->renderJson(200, '', array(
					'navTabId' => 'backendcategoryindex',
					'callbackType' => 'closeCurrent'
				));
			}

			$this->renderJson(300);
		}
		$this->isValidRequest();
		$id = intval($this->get('id'));
		$data = $this->Category->findCategory($id);
		$this->load->view('backend/category/edit', $data);
	}

	public function categorySelector() {
		$this->isValidRequest();
		$result = $this->Category->categoryList();
		$this->load->view('backend/category/selector', array('categoryList' => $result));
	}

	public function upload() {
		if (!empty($_FILES)) {
			$id = intval($this->post('id', 0));
			$source_id = intval($this->post('source_id', 0));
			$type = intval($this->post('type', 1));
			$from = intval($this->post('from', 2));
			switch($from) {
				case 3: 
					$dir = '/banner/';
					break;
				default:
					$dir = '/category/';
					break;
			}
			$config ['upload_path'] = config_item('upload_path') . $dir . date('Ymd');
			if (!is_dir($config ['upload_path'])) {
				mkdir($config ['upload_path'], 0755, true);
			}
			$config ['file_name'] = time() . rand(1000, 9999);
			$config ['allowed_types'] = 'jpg|bmp|jpeg|gif|png';

			$data = array(
				'status' => 300,
				'msg' => ''
			);

			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('filedata')) {
				$data ['msg'] = $this->upload->display_errors();
			} else {
				$dataInsert = array(
					'source_id' => $source_id,
					'img' => $config ['upload_path'] . '/' . $this->upload->data('file_name'),
					'type' => $type . '', // 类型默认链接
					'from' => $from . '', // 来源商品
				);
				if(!empty($id)) {
					$row = $this->Album->get_row(array('id'=>$id));
					if(!empty($row['img']) && file_exists($row['img'])) {
						unlink($row['img']);
					}
					$this->Album->update($dataInsert, array('id'=>$id));
					$data['id'] = $id;
				} else  {
					$dataInsert['created_at'] = time();
					$data ['id'] = $this->Album->addOne($dataInsert);
				}
				$data ['filename'] = $dataInsert['img'];
				$data ['status'] = 200;
				$data ['msg'] = base_url($dataInsert ['img']);
			}
			if ($data ['status'] == 200) {
				$this->renderJson(200, $data ['msg'], array(
					'id' => $data ['id'],
					'filename' => $data['filename'],
					'id]' => $data ['id'],
					'filename]' => $data['filename']
				));
			} else {
				show_error($data ['msg']);
			}
		}
		$this->load->view('backend/category/upload');
	}

}
